package com.stud.system.service.impl;

import com.stud.common.core.domain.entity.SysRole;
import com.stud.common.core.domain.entity.SysUser;
import com.stud.system.service.ISysMenuService;
import com.stud.system.service.ISysPermissionService;
import com.stud.system.service.ISysRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 用户权限处理
 *
 * @author fs
 */
@Service
@RequiredArgsConstructor
public class SysPermissionServiceImpl implements ISysPermissionService {

    private final ISysRoleService roleService;

    private final ISysMenuService menuService;

//    private final MyJdbcTemplate jdbcTemplate;

    /**
     * 获取角色数据权限
     *
     * @param user 用户Id
     *
     * @return 角色权限信息
     */
    @Override
    public Set<String> getRolePermission(SysUser user) {
        Set<String> roles = new HashSet<String>();
        // 管理员拥有所有权限
        if (user.isAdmin()) {
            roles.add("admin");
        } else {
            roles.addAll(roleService.selectRolePermissionByUserId(user.getUserId()));
        }
        return roles;
    }

    /**
     * 获取菜单数据权限
     *
     * @param user 用户Id
     *
     * @return 菜单权限信息
     */
    @Override
    public Set<String> getMenuPermission(SysUser user) {
        Set<String> perms = new HashSet<String>();
        // 管理员拥有所有权限
        if (user.isAdmin()) {
            perms.add("*:*:*");
        } else {
            List<SysRole> roles = user.getRoles();
            if (!CollectionUtils.isEmpty(roles)) {
                // 多角色设置permissions属性，以便数据权限匹配权限
                for (SysRole role : roles) {
                    Set<String> rolePerms = menuService.selectMenuPermsByRoleId(role.getRoleId());
                    role.setPermissions(rolePerms);
                    perms.addAll(rolePerms);
                }
            } else {
                perms.addAll(menuService.selectMenuPermsByUserId(user.getUserId()));
            }
        }
        return perms;
    }

//    @Override
//    public List<Long> getOenPitPermission(SysUser user) {
//        if (user.isAdmin()) {
//            return Collections.emptyList();
//        }
//        List<Long> openPitIds = this.jdbcTemplate.selectSingleColumn(SqlHelp.init("select open_pit_id from user_open_pit_link where user_id = ?", user.getUserId()), Long.class);
//
//        if (CollectionUtils.isEmpty(openPitIds)) {
//            if (Objects.isNull(user.getAreaId())) {
//                return Collections.emptyList();
//            }
//
//            List<Long> areaIds = areaChildrenIds(user.getAreaId());
//            if (CollectionUtils.isEmpty(areaIds)) {
//                return Collections.emptyList();
//            }
//            SqlHelp sqlHelp = SqlHelp.init("select id from safety_open_pit where area_id in (" + Toolkit.Obj2In(areaIds) + ")");
//            openPitIds = this.jdbcTemplate.selectSingleColumn(sqlHelp, Long.class);
//        }
//        return openPitIds;
//    }

//    private List<Long> areaChildrenIds(Long areaId) {
//        SqlHelp sqlHelp = SqlHelp.init();
//        sqlHelp.append(" with recursive area_cte as ( ");
//        sqlHelp.append("     select id, parent_id from safety_area where id = ? ", areaId);
//        sqlHelp.append("     union all ");
//        sqlHelp.append("     select t.id, t.parent_id from safety_area t ");
//        sqlHelp.append("     inner join area_cte cte on t.parent_id = cte.id ");
//        sqlHelp.append(" ) ");
//        sqlHelp.append(" select id from area_cte ");
//        MyJdbcTemplate jdbcTemplate = SpringUtils.getBean(MyJdbcTemplate.class);
//        return jdbcTemplate.selectSingleColumn(sqlHelp, Long.class);
//    }
}
